package com.zj.guava.eventbus.requestAndResponse;

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;

import java.util.List;

public class RequestListener {

    private ResponseListener listener;
    private EventBus bus;

    public RequestListener(EventBus bus, ResponseListener listener) {
        this.bus = bus;
        this.listener = listener;
    }

    //eventbus不适合用泛型，因为是根据入参类型来确定处理方法的。
    @Subscribe
    public void processRequest(Request req) {
        String request = (String) req.getRequest();
        System.out.println("get the request!" + request);

        bus.register(listener);
        Response response = new EventResponse("i am response:" + request);
        bus.post(response);
    }


    @Subscribe
    public void processRequest2(Request req) {
        List<String> request = (List<String>) req.getRequest();
        request.forEach(System.out::println);
        bus.register(listener);
        Response response = new EventResponse("i am response:" + request);
        bus.post(response);
    }
}
